﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Library.Logging;
using SaleManagement.Domains;
using SaleManagement.Services;

namespace SaleManagement.Web.App_Code
{
  public class BaseControl : UserControl
  {
    #region Properties
    protected string DefaultLoggingCategory
    {
      get { return BasePage.DefaultLoggingCategory; }
    }

    public string SelectedMenuItem
    {
      get { return (Page as BasePage).SelectedMenuItem; }
      set { (Page as BasePage).SelectedMenuItem = value; }
    }

    public string SelectedMenuItemText
    {
      get { return (Page as BasePage).SelectedMenuItemText; }
      set { (Page as BasePage).SelectedMenuItemText = value; }
    }

    public string SelectedMenuItemTitle
    {
      get { return (Page as BasePage).SelectedMenuItemTitle; }
      set { (Page as BasePage).SelectedMenuItemTitle = value; }
    }

    public int SelectedMenuItemIndex
    {
      get { return (Page as BasePage).SelectedMenuItemIndex; }
      set { (Page as BasePage).SelectedMenuItemIndex = value; }
    }
    #endregion

    #region Methods
    public void WriteLog(string description, Exception ex)
    {
      Logger.LogError(description, ex, DefaultLoggingCategory, null);
    }

    public bool HasPermission(int permissionId)
    {
      if (permissionId == -1) return true;
      TList<UserFunction> currentUserFunction = (Page as BasePage).CurrentUserFunctions;
      if (currentUserFunction == null)
      {
        TList<UserFunction> listUserFunction = new UserFunctionService().GetByUsername((Page as BasePage).CurrentUser.Username);
        (Page as BasePage).CurrentUserFunctions = listUserFunction;
      }
      UserFunction permission = currentUserFunction.Find(delegate(UserFunction p)
      {
        return p.FunctionId == permissionId;
      });
      if (permission != null)
      {
        return true;
      }

      return false;
    }

    public string GetItemDescription(object status, string path)
    {
      return ((Page) as BasePage).GetItemDescription(status, path);
    }

    #endregion
  }
}
